package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.DuDaoLieBiaoBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.DuDaoAdapter;
import com.oscar.ehome.view.pageview.view.DuDaoLieBiaoView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: SunHaoZhao
 * date: On 2018/1/18
 */

public class DuDaoLieBiao extends BasesActivity<DuDaoLieBiaoView, PagePresenter> implements AdapterView.OnItemClickListener, XRefreshView.XRefreshViewListener, DuDaoLieBiaoView {
    PagePresenter pagePresenter;
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;
    @BindView(R.id.title_img_right_dudao)
    ImageView titleImgRightDudao;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.dudaoliebiao)
    MyList dudaoliebiao;
    @BindView(R.id.huoke_xrefresh)
    XRefreshView huoke_xrefresh;
    public static long lastRefreshTime;
    String token;
    DuDaoAdapter adapter;
    private List<DuDaoLieBiaoBean.dudaoitem> lists = new ArrayList<DuDaoLieBiaoBean.dudaoitem>();//数据源

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dudaolistnew);
        ButterKnife.bind(this);


        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(DuDaoLieBiao.this).getSystemConfig().getColor()));
        initView();

    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("督导");
        token = SystemConfigFactory.getInstance(DuDaoLieBiao.this).getSystemConfig().getToken();
        pagePresenter = new PagePresenter(this);
        titleLayoutLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        pagePresenter.getdudaoliebiao(token);
        adapter = new DuDaoAdapter(DuDaoLieBiao.this, lists);
        dudaoliebiao.setAdapter(adapter);
        showLoading();
        dudaoliebiao.setOnItemClickListener(this);
        huoke_xrefresh.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        huoke_xrefresh.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        huoke_xrefresh.setPullLoadEnable(true);
        // 设置上次刷新的时间
        huoke_xrefresh.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        huoke_xrefresh.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        huoke_xrefresh.setAutoRefresh(false);
    }

    @Override
    public void getDuDaoLieBiao(DuDaoLieBiaoBean result) {
        dismissLoading();
        huoke_xrefresh.stopRefresh();
        if (result != null) {


            lists = result.getList();
            if (lists.size() > 0) {
                adapter.setLists(lists);
            }

            huoke_xrefresh.setPullLoadEnable(false);

        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(DuDaoLieBiao.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(DuDaoLieBiao.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(DuDaoLieBiao.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(DuDaoLieBiao.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(DuDaoLieBiao.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(DuDaoLieBiao.this, DuDaoXiangQingActivity.class);
        if (lists.get(position).getValue() .equals("1")) {
            intent.putExtra("type", "1");
        } else if (lists.get(position).getValue() .equals("2")) {
            intent.putExtra("type", "2");
        }else{
            intent.putExtra("type", "3");
        }

        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        lastRefreshTime = huoke_xrefresh.getLastRefreshTime();
        huoke_xrefresh.restoreLastRefreshTime(lastRefreshTime);

        pagePresenter.getdudaoliebiao(token);
    }

    @Override
    public void onRefresh(boolean isPullDown) {

    }

    @Override
    public void onLoadMore(boolean isSilence) {

    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
}
